package com.chenglai.slideshow.service.impl;

import com.chenglai.commonutils.common.DateFormatHelper;
import com.chenglai.commonutils.common.OSSUtil;
import com.chenglai.slideshow.dao.SlideshowMapper;
import com.chenglai.slideshow.pojo.Slideshow;
import com.chenglai.slideshow.service.SlideshowService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;

@Service
public class SlideshowServiceImpl implements SlideshowService {

    @Resource
    private SlideshowMapper slideshowMapper;

    @Override
    public List<Slideshow> getSlideshow(Integer shopId, Integer flag, Integer type) {
        Example example = new Example(Slideshow.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("shopId",shopId).andEqualTo("slideshowType",type);
        example.orderBy("slideshowFlag").desc().orderBy("slideshowTime").desc();
        return slideshowMapper.selectByExample(example);
    }

    @Override
    public void addSlideshow(Slideshow slideshow, HttpServletRequest request) {
        // Pic等于null 就代表是第一次上次而非图库
        if (slideshow.getSlideshowPic() ==null){
            MultipartFile pic = ((MultipartRequest) request).getFile("file");
            String fileName = OSSUtil.uploadingImg(pic,"shangcheng/slideshow");
            slideshow.setSlideshowPic(fileName);
        }
        slideshow.setSlideshowStatus(1);
        slideshow.setSlideshowTime(DateFormatHelper.currentTimes());
        slideshowMapper.insertSelective(slideshow);
    }

    @Override
    public PageInfo getSlideshowList(Integer page, Integer size, Integer shopId) {
        PageHelper.startPage(page, size);
        Example example = new Example(Slideshow.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("shopId",shopId);
        example.orderBy("slideshowId").desc();
        return new PageInfo(slideshowMapper.selectByExample(example));
    }

    @Override
    public void delSlideshow(Integer slideshowId, String picName) {
        Example example = new Example(Slideshow.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("slideshowPic",picName);
        int i = slideshowMapper.selectCountByExample(example);
        //是图库最后一张图片时删除掉OSS服务器上的图片
        if (i == 1){
            OSSUtil.deleteFile(picName);
        }
        slideshowMapper.deleteByPrimaryKey(slideshowId);
    }

    @Override
    public void updateSlideshowStatus(Integer status, String ids) {
        String[] split = ids.split(",");
        Example example = new Example(Slideshow.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("slideshowId", Arrays.asList(split));
        Slideshow slideshow = new Slideshow();
        slideshow.setSlideshowStatus(status);
        slideshowMapper.updateByExample(slideshow,example);
    }

    @Override
    public void updateSlideshowFlag(String ids, Integer flag) {
        String[] split = ids.split(",");
        Example example  = new Example(Slideshow.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("slideshowId", Arrays.asList(split));
        Slideshow slideshow = new Slideshow();
        slideshow.setSlideshowFlag(flag);
        slideshowMapper.updateByExample(slideshow,example);
    }

    @Override
    public List<String> getSlideshowLibrary(Integer shopId) {

        return slideshowMapper.getSlideshowLibrary(shopId);
    }
}
